<template>
  <div class="page-content">
    <div class="header" style="margin-bottom: 1.5rem">
      <h3>班型订单</h3>
    </div>
    <!-- 表格菜单 -->
    <el-radio-group v-model="tableLayout" @change="statusChange">
      <el-radio-button value="">全部</el-radio-button>
      <el-radio-button value="0">待支付</el-radio-button>
      <el-radio-button value="1">已支付</el-radio-button>
      <el-radio-button value="2">已关闭</el-radio-button>
    </el-radio-group>
    <!-- 表格菜单 -->
    <el-row style="margin-top: 1.5rem; width: 100%">
      <!-- <el-col :xs="24" :sm="12" :lg="6">
        <el-input placeholder="部门名称"></el-input>
      </el-col> -->
      <div style="width: 12px"></div>
      <!-- 顶部搜索栏 -->
      <div style="width: 100%">
        <el-form :model="searchForm" ref="searchFormRef" label-width="82px">
          <el-row :gutter="24">
            <el-col :span="6">
              <el-form-item label="订单号：" prop="queryCondition">
                <el-input placeholder="请输入订单号" v-model="searchForm.queryCondition" clearable @blur="getData"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="商品信息：" prop="shoppingOrderId">
                <el-input placeholder="请输入商品信息" v-model="searchForm.queryMemberName" clearable
                  @blur="getData"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="订单来源：" prop="source">
                <el-select v-model="searchForm.source" placeholder="请输入订单来源" clearable @change="getData">
                  <el-option v-for="item in typeOptions_two" :key="item.value" :label="item.label"
                    :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="支付时间：">
                <el-date-picker label="支付时间：" v-model="datavalue1" type="datetimerange" start-placeholder="开始日期"
                  end-placeholder="结束日期" format="YYYY-MM-DD HH:mm:ss" date-format="YYYY/MM/DD ddd"
                  time-format="A hh:mm:ss" @change="orderTimeChange" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!-- end顶部搜索栏 -->

      <div> </div>
    </el-row>
    <art-table :data="tableData" :loading="tableLoading" :currentPage="pageNum" :pageSize="pageSize" :total="pageTotal"
      @current-change="currentChange" @size-change="sizeChange">
      <template #default>
        <el-table-column type="selection" width="55" />
        <!--        <el-table-column label="ID" prop="id" width="55" />-->
        <!-- 展示订单图片和名字等 -->
        <el-table-column label="班型信息" align="center">
          <template #default="scope">
            {{ scope.row.shoppingOrderName }}
          </template>
        </el-table-column>
        <el-table-column label="车型" prop="carName" align="center" />
        <el-table-column label="应付金额" align="center" width="100">
          <template #default="scope">
            {{ Number(scope.row.shoppingOrderPrice).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="实付金额" prop="amount" align="center" width="100">
          <template #default="scope">
            {{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column label="支付方式" prop="paymentMethod" align="center">
          <template #default="score">
            {{
              score.row.paymentMethod === 1
                ? '支付宝支付'
                : score.row.paymentMethod === 2
                  ? '微信支付'
                  : score.row.paymentMethod === 3
                    ? '银行卡支付'
                    : score.row.paymentMethod === 4
                      ? '现金支付'
                      : score.row.paymentMethod === 5
                        ? '活动赠送'
                        : ''
            }}
          </template>
        </el-table-column>
        <el-table-column label="订单来源" prop="orderChannel" align="center">
          <template #default="score">
            {{
              score.row.orderChannel === 0
                ? 'App'
                : score.row.orderChannel === 1
                  ? '微信公众号'
                  : score.row.orderChannel === 2
                    ? '微信小程序'
                    : score.row.orderChannel === 3
                      ? '线下'
                      : score.row.orderChannel === 4
                        ? '兑换码兑换'
                        : score.row.orderChannel === 5
                          ? '新人礼包发放'
                          : score.row.orderChannel === 6
                            ? '老带新'
                            : ''
            }}
          </template>
        </el-table-column>
        <el-table-column label="下单时间：" prop="createdAt" align="center">
          <template #default="scope">
            {{ formatDate(scope.row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column label="订单号" prop="ordersNo" align="center" />
        <el-table-column label="报名信息">
          <template #default="scope">
            <div> 姓名：{{ scope.row.userNameTwo }}</div>
            <div> 手机号：{{ scope.row.userMobile }} </div>
            <div> 报名车型：{{ scope.row.carName }} </div>
          </template>
        </el-table-column>
        <el-table-column label="用户信息" prop="userName" align="center">
          <template #default="scope">
            <div> {{ scope.row.userName }}</div>
            <div>
              ID：{{ scope.row.userId }}
            </div>
            <div> 手机号：{{ scope.row.shoppingOrderMobile }} </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template #default="scope">
            <el-button size="small" round plain type="info" v-if="scope.row.status === 0">
              待支付
            </el-button>
            <el-button size="small" round plain type="primary" v-if="scope.row.status === 1">
              已支付
            </el-button>
            <el-button size="small" round plain type="warning" v-if="scope.row.status === 2">
              已取消
            </el-button>
            <el-button size="small" round plain type="success" v-if="scope.row.status === 3">
              已完成
            </el-button>
            <el-button size="small" round plain type="danger" v-if="scope.row.status === 4">
              已退款
            </el-button>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100px" align="center">
          <template #default="scope">
            <el-button type="primary" link v-if="scope.row.status === 1"
              @click="handleRefund(scope.row.ordersNo, '02')">退款</el-button>
          </template>
        </el-table-column>
      </template>
    </art-table>
  </div>
</template>

<script setup lang="ts">
import { ElMessageBox, FormInstance } from 'element-plus'
import { vipOrders, vipType } from '@/api/userStudentsApi'
import { orderRefund } from '@/api/ApiList'
const tableLayout = ref('')

const formatDate = (date: string) => {
  return new Date(date)
    .toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
    .replace(/\//g, '-')
}

// 使用所有题库列表的ref
let tableData = ref([])

function statusChange() {
  searchForm.status = tableLayout.value
  getData()
}

//搜索刷新等
const search = () => {
  getData()
}
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
  datavalue1.value = ''
  searchForm.queryStartTime = ''
  searchForm.queryEndTime = ''
}
const searchFormRef = ref<FormInstance>()
const columns = reactive([
  { name: '用户信息', show: true },
  { name: '手机号', show: true },
  { name: '性别', show: true },
  { name: '来源', show: true },
  { name: '状态', show: true },
  { name: '创建日期', show: true }
])
const changeColumn = (list: any) => {
  columns.values = list
}
interface searchItem {
  queryCondition: string
  queryStartTime: string
  queryEndTime: string
  status?: string
  source: string
  queryMemberName: string
  carModelId: string
}
const searchForm = reactive<searchItem>({
  queryCondition: '',
  queryMemberName: '',
  carModelId: '',
  source: '',
  queryStartTime: '',
  queryEndTime: ''
})

const typeOptions_two = [
  {
    value: '0',
    label: '兑换码兑换'
  },
  {
    value: '1',
    label: '新人礼包发放'
  },
  {
    value: '2',
    label: '老带新'
  },
  {
    value: '3',
    label: '线上购买'
  }
]

const datavalue1 = ref('') //创建时间：
const orderTimeChange = (e: any) => {
  if (e) {
    searchForm.queryStartTime = formatDate(e[0])
    searchForm.queryEndTime = formatDate(e[1])
  } else {
    searchForm.queryStartTime = ''
    searchForm.queryEndTime = ''
  }
  getData()
}

const tableLoading = ref(false)
const pageNum = ref(1)
const pageSize = ref(10)
const pageTotal = ref(0)
const filterEmptyValues = (obj: any) => {
  const result: any = {}
  for (const key in obj) {
    if (obj[key] !== '' && obj[key] !== null && obj[key] !== undefined) {
      result[key] = obj[key]
    }
  }
  return result
}

function handleRefund(ordersNo: string, type: string) {
  ElMessageBox.confirm('确定退款吗？', '确认', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    handleOrderRefund(ordersNo, type)
  })
}

async function handleOrderRefund(ordersNo: string, type: string) {
  try {
    const res = await orderRefund({
      ordersNo,
      type
    })
    if (res.code === '00000') {
      ElMessage.success('退款成功')
      getData()
    }
  } catch {
  } finally {
  }
}

async function getData() {
  try {
    const params = filterEmptyValues(searchForm)
    const { code, data } = await vipOrders({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      type: 5,
      ...params
    })
    if (code === '00000' && data) {
      tableData.value = data.list
      pageTotal.value = data.total
    }
  } catch (e) {
  } finally {
    tableLoading.value = false
  }
}
const currentChange = (val: number) => {
  pageNum.value = val
  getData()
}

const sizeChange = (val: number) => {
  pageSize.value = val
  pageNum.value = 1
  getData()
}

const vipData = ref([])
const getVipList = async () => {
  const res = await vipType()
  if (res.code === '00000') {
    vipData.value = res.data
  }
}

onMounted(() => {
  getData()
  getVipList()
})
</script>

<style lang="scss" scoped>
.page-content {
  .svg-icon {
    width: 1.8em;
    height: 1.8em;
    overflow: hidden;
    vertical-align: -8px;
    fill: currentcolor;
  }
}

.tag-item+.tag-item {
  margin-left: 5px;
}

.label {
  float: left;
  margin: 0.5rem 0 0.5rem 1.5rem;
  display: inline;
}

.labellist .label:nth-child(1) {
  margin-left: 0;
}

.bankcont_title_text {
  float: left;
  // margin-right: 1rem;
  // font-weight: bold;
}

.label_sonli {
  color: #5d87ff;
}

.tag-item {
  margin-left: 5px;
}

.tagcontsize {
  color: #999;
}

.tagcontbigsize {
  margin-top: 0.5rem;
}

.cartcont {
  margin-top: 0.2rem;
}

.tableimg {
  width: 100%;
  height: 100%;
}

.title_box_left {
  float: left;
  width: 8rem;
  height: 4.5rem;
  border-radius: 0.5rem;
  overflow: hidden;
  margin-right: 1rem;
}

.title_box_right {
  float: right;
  float: left;
}

.bankcont_title_text {
  height: 2rem;
  margin-top: 0.3rem;
}

.columnvideo {
  border-radius: 0.5rem;
}
</style>
